package com.sali.b_design_pattern.g_singleton_pattern.lesson_destroy_singleton.b_serialize;

import java.io.Serializable;

/**
 * @description:
 * @date: 2023/3/19 12:28.
 * @version: 1.0
 */
public class HungrySingletonSafe implements Serializable {

    /**
     * description: 饿汉单例对象：告诉我们在这个类加载时，即会在内存空间中申请一个地址出来存放实例
     **/
    private static HungrySingletonSafe instance = new HungrySingletonSafe();

    /**
     * description: 构造函数私有化
     **/
    private HungrySingletonSafe() {
        if (instance != null) {
            throw new RuntimeException("不允许创建多个实例");
        }
    }

    /**
     * description: 返回单例对象
     **/
    public static HungrySingletonSafe getInstance() {
        return instance;
    }

    private Object readResolve() {
        return instance;
    }
}
